package org.yray.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class HelloServer {

    public static void main(String[] args) {

        //定义主线程组，用于接收客户端请求，但是并不做任何逻辑处理
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        //从线程组，主线程组会把相应的请求转交给该线程组，由从线程组去做任务
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        //创建netty服务器
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        try {
            //服务器设置绑定两个线程组，并设置相应的助手类【handler】
            serverBootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)				//设置nio双向通道
                    .childHandler(new HelloServerInitializer());		//子处理器

            //启动server并绑定端口号
            ChannelFuture channelFuture = serverBootstrap.bind(8088).sync();
            //关闭监听的channel
            channelFuture.channel().closeFuture().sync();
        } catch (Exception e) {
            System.out.println("err");
        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }

    }

}
